package com.szrt.day20170427.designmodel.demo04;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理  类比
 * @author zsrt
 *
 */
public class DynProxyBak implements  InvocationHandler {
	
	
	//目标对象
	private Object targetObject=null;
	
	
	
	public DynProxyBak(Object targetObject){
		this.targetObject = targetObject;
	}
	
	
	public Object getaab (){
		
		ClassLoader loader = targetObject.getClass().getClassLoader() ;
		
		InvocationHandler h = this;
		System.err.println(h.getClass().getName());
		Class<?>[] interfaces=targetObject.getClass().getInterfaces();
		
		System.out.println("1");
		Object proxy  = Proxy.newProxyInstance(loader, interfaces, h);
		System.out.println("2");
		
		return proxy;
	}

	
	/**
	 * 当执行 Proxy.newProxyInstance(loader, interfaces, h) 会 回调  调用方法 invoke
	 */
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.err.println("记录开始时间...");
		long begin = System.currentTimeMillis();
		
		
		//方法的调用 invoke是一个抽象方法
		//Object obj = method.
		
		Object object = method.invoke(targetObject, args);
		
		long end = System.currentTimeMillis();
		System.err.println("记录结束时间..");
		System.out.println(end-begin);
		return object;
	}

}
